package com.yaodian100.mall.oauth360;

import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.geronimo.mail.util.Base64;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.yaodian100.core.common.utils.ServerValue;
import com.yaodian100.mall.member.util.UserData;

/**
 * 
 * @author aaron.chueh
 * 
 */
public class Oauth360Action extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID=5680934928218116964L;
	
	//toOauth360Login()
	private String redirect_url;
	
	//Oauth360Return()
	private String code;

	public String toOauth360Login() throws Exception{
		HttpServletRequest request=(HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		HttpServletResponse response=(HttpServletResponse) ActionContext.getContext().get(ServletActionContext.HTTP_RESPONSE);
		
		if(StringUtils.isBlank(redirect_url)){
			redirect_url=ServerValue.getFrontHostname()+ServerValue.getFrontContextPath()+"/member/myyaodian.do";
		}
		redirect_url=new String(Base64.encode(redirect_url.getBytes()));
		
		UserData userData=new UserData(request,response);
		userData.writeRawValue(UserData.NAME_YAODIAN100_360_PATHPARAM,redirect_url);
		
		String clientId=Oauth360Config.CLIENT_ID;
		String callbackUrl=Oauth360Config.getEncoderCallbackUrl();
		String toOauth360Url="https://open.i.360.cn/oauth/authorize/?response_type=code&client_id="+clientId
			+"&redirect_uri="+callbackUrl;
		
		response.sendRedirect(toOauth360Url);
		return null;
	}

	public String oauth360Return() throws Exception {
		HttpServletRequest request=(HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		HttpServletResponse response=(HttpServletResponse) ActionContext.getContext().get(ServletActionContext.HTTP_RESPONSE);
		
		UserData userData=new UserData(request,response);
		String pathParam=userData.readRawValue(UserData.NAME_YAODIAN100_360_PATHPARAM);
		
		Oauth360Utils oauth360Utils=new Oauth360Utils();
		String accessToken=oauth360Utils.getAccessToken(code);
		
		JSONObject userInfo=oauth360Utils.getUserInfo(accessToken);
		String id=(String)userInfo.get("id");
		if(id!=null && id.trim().length()>0){
			String name=(String)userInfo.get("name");
			name=new String(name.getBytes("utf-8"),"utf-8");
			name=URLEncoder.encode(name,"utf-8");
			
			String loginUrl=ServerValue.getFrontContextPath()+"/hao360Login.do?loginID="+id+"&contactName="
							+name+"&pathparam="+pathParam;

			response.sendRedirect(loginUrl);
		}else{
			response.sendRedirect(ServerValue.getFrontHostname());
		}
		
		return null;
	}

	// getter & setter
	public String getRedirect_url() {
		return redirect_url;
	}

	public void setRedirect_url(String redirect_url) {
		this.redirect_url = redirect_url;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}
}